/**
 * phi
 */

TM.loadScript("app");
TM.loadScript("geom");

var SCREEN_WIDTH = 640;
var SCREEN_HEIGHT= 480;

var TARGET = (function(){
    var hash = document.location.hash.substr(1);
    if (!hash) { hash = "Enchant.js"; }
    return hash;
})();


var PROBLEM_DATA_LIST = {
    "JavaScript String" : JS_STRING_PROBLEM,
    "Enchant.js"        : ENCHANT_JS_PROBLEM,
    "Enchant.js Sprite" : ENCHANT_JS_SPRITE_PROBLEM,
    "HTML5 Section"     : HTML5_SECTION_PROBLEM,
};
var PROBLEM_DATA = PROBLEM_DATA_LIST[TARGET];


TM.main(function() {
    var canvas = TM.$id("c");
    TM.$centering(canvas);
    
    var app = TM.App.CanvasApp(canvas);
    app.scene.background = "rgb(0, 0, 0)";
    
    // サウンド読み込み
    TM.$SoundManager.add("type", "sound/type00.ogg", 8);
    
    // 問題クラスを作成
    var Problem = TM.createClass({
        superClass: TM.App.GameElement,
        
        init: function(param) {
            this.superInit(TM.App.GameElement);
            
            // ワード
            this.word = TM.App.Text();
            this.word.align     = "center";
            this.word.baseline  = "center";
            this.word.width     = 640;
            this.addChild(this.word);
            
            // 説明
            this.description = TM.App.Text();
            this.description.align     = "center";
            this.description.baseline  = "center";
            this.description.width     = 640;
            this.description.size = 16;
            this.addChild(this.description);
            
            // 画像用スプライト
            this.imgSprite = TM.App.Sprite();
            this.imgSprite.x = 320;
            this.imgSprite.y = 240;
            this.imgSprite.width = 128;
            this.imgSprite.height = 128;
            this.addChild(this.imgSprite);
            
            // 画像キャッシュ領域
            this.imgCache = {};
            
            this.reset(param);
        },
        
        update: function(app) {
            var ch = this.word.text[0];
            if ( app.keyboard.getKeyDown(ch) ) {
                this.word.text = this.word.text.substr(1);
                // SE
                var se = TM.$SoundManager.get("type");
                se.volume = 0.25;
                se.play();
            }
        },
        
        reset: function(param) {
            var self = this;
            
            this.word.text = param.word;
            this.word.position.set(320, 240);
            
            this.description.text = param.description;
            this.description.position.set(320, 200);
            
            this.imgSprite.position.set(320, 120);
            if (param.img) {
                if (this.imgCache[param.img]) {
                    self.imgSprite.graphics.clear();
                    self.imgSprite.graphics.drawImage(this.imgCache[param.img], -self.imgSprite.width/2, -self.imgSprite.height/2);
                }
                else {
                    var img = new Image();
                    img.src = param.img;
                    
                    img.addEventListener("load", function() {
                        self.imgSprite.graphics.clear();
                        self.imgSprite.graphics.drawImage(img, -self.imgSprite.width/2, -self.imgSprite.height/2);
                    }, false);
                    
                    // キャッシュ
                    this.imgCache[param.img] = img;
                }
            }
        },
        
        isClear: function() { return this.word.text == ""; }
    });
    
    
    
    var problem = Problem(PROBLEM_DATA[0]);
    
    // メイン処理
    app.scene.update = function() {
        if (problem.isClear() == true) {
            problem.reset(PROBLEM_DATA[TM.random(0, PROBLEM_DATA.length)]);
        }
    };
    
    app.scene.addChild(problem);
    
    app.run();
});
